﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Xml.Linq;
using Apps.Yunwei.YunWeiService;

namespace Apps.Yunwei.Views
{
    public partial class ServicePanel : UserControl
    {
        public ServicePanel()
        {
            InitializeComponent();
            RequestDirs();
        }
        public ServiceObject Contact
        {
            get { return (ServiceObject)this.DataContext; }
        }
        const string BASE_DIR = "clientbin";
        // 请求图片目录清单
        private void RequestDirs()
        {
            Q.ConfigManager.Request(Q.ConfigManager.GetUrl("maptypes"), (s, ea) =>
            {
                XDocument doc = XDocument.Parse(ea);

                if (s)
                {
                    var d = from element in doc.Root.Elements()
                            select new { Value = element.Attribute("value").Value, Key = element.Attribute("key").Value };
                    cmbType.ItemsSource = d;
                    if (this.DataContext != null && this.DataContext is ServiceObject)
                    {
                        cmbType.SelectedValue = (this.DataContext as ServiceObject).Type;
                    }
                }
                else
                {
                    this.ShowMessage(ea);
                }
            });
        }


        private void ResumeEvent(bool result)
        {
            if (Done != null)
            {
                Done(this, new EditEventArgs() { Result = result});
            }
        }
        private void OnOKClick(object sender, RoutedEventArgs e)
        {
            ResumeEvent(true);
        }

        private void OnCancelClick(object sender, RoutedEventArgs e)
        {
            ResumeEvent(false);
        }

        private void ChoiceImage(string key)
        {
            new ImageSelector() { DataContext = this.DataContext, Source = key }.Show();
        }
        private void ChoiceColor(int mode)
        {
            new ColorSelector(mode) { DataContext = this.DataContext }.Show();
        }

        public event EventHandler<EditEventArgs> Done;

        private void ChoiceImageClick(object sender, RoutedEventArgs e)
        {
            ChoiceImage("Icon1");
        }

        private void ChoiceBackgroundColorClick(object sender, System.Windows.RoutedEventArgs e)
        {
            ChoiceColor(1);
        }
        private void ChoiceForegroundColorClick(object sender, System.Windows.RoutedEventArgs e)
        {
            ChoiceColor(2);
        }
    }
}